package cn.jobstart.config.swagger;

import cn.jobstart.common.StringUtil;
import cn.jobstart.web.common.SkJsonResult;
import cn.jobstart.web.common.token.JwtsUtil;
import com.alibaba.fastjson.JSONObject;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2022-11-24 09:55
 */

@Component
public class SkDocket {

    @Value("${swaggerShow}")
    private boolean swaggerShow;

    private List<Parameter> initPars() {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name(JwtsUtil.ADMIN_HEADER_KEY).description("用户令牌")
                .modelRef(new ModelRef("string")).parameterType("header").scalarExample("test")
                .required(true).build(); //header中的ticket参数非必填，传空也可以
        pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数
        ticketPar = new ParameterBuilder();
        ticketPar.name("jsonType").description("jsonType")
                .modelRef(new ModelRef("string")).parameterType("header").scalarExample("jsonType")
                .required(false).defaultValue("jsonType").build(); //header中的ticket参数非必填，传空也可以
        pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数

        return pars;

    }

    public Docket getDocketInstanceByController(String groupName, Predicate<RequestHandler>... predicate) {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .ignoredParameterTypes(HttpServletRequest.class, HttpSession.class, HttpServletResponse.class)
                .select()
                .apis(Predicates.or(predicate))
                .paths(PathSelectors.any())
                .build()
                .enable(swaggerShow)
                .globalOperationParameters(initPars());
        if (StringUtil.isNotObjEmpty(groupName)) {
            docket.groupName(groupName);
        }

        return docket;
    }

    public Docket getDocketInstanceByPaths(String groupName, String paths) {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .ignoredParameterTypes(HttpServletRequest.class, HttpSession.class, HttpServletResponse.class)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant(paths))
                .build()
                .enable(swaggerShow)
                .globalOperationParameters(initPars());


        if (StringUtil.isNotObjEmpty(groupName)) {
            docket.groupName(groupName);
        }
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()

                .title("宁波人才 RESTful APIs")
                .description("宁波人才接口文档<br/>返回数据格式:" + JSONObject.toJSONString(SkJsonResult.ok().setData(new Object())))
                .version("1.0")

                .build();
    }


}
